<?php
//连接数据库
include("config/config.php");
switch ($_POST['handle']) {
    case 'get':
       echo get($dbh);
        break;
    case 'login':
        echo login($dbh);
        break;
    case 'dele':
        echo dele($dbh);
        break;
    case 'dele_novel':
        echo dele_novel($dbh);
        break;
    case 'add':
        echo add($dbh);
        break;
    case 'add_novel':
        echo add_novel($dbh);
        break;
    case 'modify':
        echo modify($dbh);
        break;
    case 'modify_novel':
        echo modify_novel($dbh);
        break;
    case 'logout':
        echo logout();
        break;
    case 'admin':
        echo admin();
        break;
    case 'admin_logout':
        echo admin_logout();
        break;
    default:
        exit;
        break;
}
function get($dbh){
    $novel=$dbh->query("SELECT * FROM novel WHERE name='".$_POST['name']."'")->fetchAll();
    if($novel){
        return $_POST['name']."||done";
    }
    else{
        return $_POST['name']."||no";
    }
}
function login($dbh){
    $user=$dbh->query("SELECT * FROM user WHERE username='".$_POST['username']."' AND password='".md5($_POST['password'])."'")->fetchAll();
    if($user){
        session_start();
        $_SESSION['username']=$_POST['username'];
        return "done";
    }
    else{
        return "no";
    }
}
function logout(){
    session_start();
    unset($_SESSION['username']);
    return "done";
}
function dele($dbh){
    $count = $dbh->exec("DELETE FROM user WHERE id=".$_POST['id']);
    if($count){
        return "done";
    }
}

function dele_novel($dbh){
    $count = $dbh->exec("DELETE FROM novel WHERE id=".$_POST['id']);
    if($count){
        return "done";
    }
}
function add($dbh){
    $count = $dbh->exec("INSERT INTO user SET username='".$_POST['username']."' ,name='".$_POST['name']."',score='".$_POST['score']."',email='".$_POST['email']."' ,time=NOW()");
    if($count){
        return "done";
    }
}
function add_novel($dbh){
    $count = $dbh->exec("INSERT INTO novel SET name='".$_POST['name']."' ,novel='".$_POST['novel']."' ,img='".$_POST['img']."' ,author='".$_POST['author']."' ,type='".$_POST['type']."' ,status='".$_POST['status']."' ,hot='".$_POST['hot']."' ,publish='".$_POST['publish']."' ,content='".$_POST['content']."' ,time=NOW()");
    if($count){
        return "done";
    }
}
function modify($dbh){
    $count = $dbh->exec("UPDATE user SET username='".$_POST['username']."' ,name='".$_POST['name']."',score='".$_POST['score']."',email='".$_POST['email']."' ,time=NOW() WHERE id='".$_POST['id']."'");
    if($count){
        return "done";
    }
}
function modify_novel($dbh){

    $count = $dbh->exec("UPDATE novel SET name='".$_POST['name']."' ,novel='".$_POST['novel']."' ,img='".$_POST['img']."' ,author='".$_POST['author']."' ,type='".$_POST['type']."' ,status='".$_POST['status']."' ,hot='".$_POST['hot']."' ,publish='".$_POST['publish']."' ,content='".$_POST['content']."' ,time=NOW() WHERE id='".$_POST['id']."'");
    if($count){
        return "done";
    }
}
function admin(){
    if($_POST['username']=="admin" && $_POST['password']=="admin"){
        session_start();
        $_SESSION['admin']="admin";
        return "done";
    }
}
function admin_logout(){
    session_start();
    unset($_SESSION['admin']);
    return "done";
}